
import os
from meta_service.tag import ArxivTag,InitTag
from extra_service.institution import CACHE_DIR

def _search_pdf_file_in_dir(entry_id:str,dir_path:str):
    """
    在指定目录中搜索pdf文件
    """
    import os
    entry_id_file = f"{entry_id}.pdf"
    all = os.listdir(dir_path)
    count_all = len(all)
    try:
        idx = all.index(entry_id_file)
        if idx < 0 or idx >= count_all:
            return None
        return os.path.join(dir_path,entry_id_file)
    except ValueError:
        return None

def _search_pdf_file_in_institution_cache_dir(entry_id:str):
    """
    在缓存目录中搜索pdf文件
    """
    return _search_pdf_file_in_dir(entry_id,CACHE_DIR)

def SearchPdfFile(entry_id:str,search_dir=None):
    """
    搜索pdf文件,返回的是带有search_dir的pdf文件路径
    """
    pdf_file = None
    if search_dir is None:
        search_dir = CACHE_DIR
    pdf_file = _search_pdf_file_in_dir(entry_id,search_dir)
    return pdf_file # 这里返回的是带有search_dir的pdf文件路径

def GetFullPdfPath(entry_id:str,search_dir=None):
    """
    获取完整的pdf路径
    """
    file_path = SearchPdfFile(entry_id,search_dir)
    if file_path is None:
        return None
    return os.path.abspath(file_path)

